﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Service.Entity.Core.Enumerations;
using Kevin.Shop.Business.Service.Entity.Core.Member;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Component;
using Kevin.Shop.Business.Service.Entity.StoreManagement.Customer;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.Service.Entity.SalesManagement.PromotionCode;

namespace RiderShop.Mobile.Modle.MyAccount
{
    public class MyPointModel
    {
        public List<CustomerPointInfo> PointList { get; set; }

        public DbQueryResult<List<CustomerPointInfo>> GetUserPoint(int pageIndex, int pageSize)
        {
            string url = SiteUrl.FormatUrl("Common_GetUserOrderPointsInfo");
            var query = new CustomerPointQuery()
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                UserId = WebClientContext.Current.UserId
            };
            DbQueryResult<List<CustomerPointInfo>> result = RestfulHelper.Query<CustomerPointInfo>(url, query);
            if (result.Data.Count > 0)
            {
                PageIndex = pageIndex;
                PageSize = pageSize;
                TotalCount = result.TotalRecords;
                TotalPage = (int)Math.Ceiling(TotalCount / (double)PageSize);
                PointList = result.Data;
                return result;
            }

            return null;
        }


        public DbQueryResult<List<CustomerPointInfo>> GetUserPoint(CustomerPointQuery query)
        {
            string url = SiteUrl.FormatUrl("Common_GetUserOrderPointsInfo");

            DbQueryResult<List<CustomerPointInfo>> result = RestfulHelper.Query<CustomerPointInfo>(url, query);
            if (result.Data.Count > 0)
            {
                PageIndex = query.PageIndex;
                PageSize = query.PageSize;
                TotalCount = result.TotalRecords;
                TotalPage = (int)Math.Ceiling(TotalCount / (double)PageSize);
                PointList = result.Data;
                return result;
            }

            return null;
        }

        public ServiceResponser UserCheckIn()
        {
            string url = SiteUrl.FormatUrl("Common_ProcessCustomerPointRequest");
            CustomerPointRequest request = new CustomerPointRequest()
            {
                UserId = WebClientContext.Current.UserId,
                RuleId = Kevin.Shop.Business.Service.Entity.StoreManagement.Enumerations.CustomerPointRuleType.CheckIn
            };

            var res = RestfulHelper.Update<CustomerPointRequest>(url, request);
            res.Tag = WebClientContext.Current.GetCurrentUser(false).Points.ToString();
            return res;
        }

        public ServiceResponser CheckInRewards(bool checkOnly)
        {
            string url = SiteUrl.FormatUrl("SalesManagement_PointCheckInRewards", WebClientContext.Current.UserId, checkOnly);

            var res = RestfulHelper.UpdateWithQueryString(url);
            return res;
        }

        //是否有上一页
        public bool HasPreviousPage
        {
            get { return (PageIndex > 0); }
        }

        //是否有下一页
        public bool HasNextPage
        {
            get { return (PageIndex + 1 < TotalPage); }
        }

        //页面索引值
        private int _pageIndex;
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }

        //每页记录的数量
        private int _pageSize;
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        //总记录数
        private int _totalCount;
        public int TotalCount
        {
            get { return _totalCount; }
            set { _totalCount = value; }
        }

        //总页数
        private int _totalPage;
        public int TotalPage
        {
            get { return _totalPage; }
            set { _totalPage = value; }
        }
    }
}
